<template>
  <div>
    <el-upload
      action="#"
      :file-list="fileList"
      :class="{disable:fileList.length>0}"
      list-type="picture-card"
      :on-preview="onPreview"
      :on-change="onChange"
      :on-remove="onRemove"
      :http-request="upload"
      :before-upload="beforeUpload"
      :on-exceed="onExceed"
    >
      <i class="el-icon-plus" />
    </el-upload>

    <!-- 进度条 -->
    <el-progress
      v-if="isShowPercent"
      :text-inside="true"
      :stroke-width="24"
      :percentage="percent"
      style="width:150px;margin:5px 0 0 40px"
    />

    <!-- 预览效果 -->
    <el-dialog title="预览" :visible="isShowDialog" @close="isShowDialog=false">
      <el-row type="flex" justify="center">
        <img :src="previewURL" alt="" style="width:300px;height:300px">
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
// 引入库
import COS from 'cos-js-sdk-v5'
// 创建实例
const cos = new COS({
  SecretId: 'AKIDqUt1sSlpdAQQ9bmhMkrhHacNs74kAJBm',
  SecretKey: 'LQFn32kBMIyNVMxIM9gwwf6G8j49xUji'
})
export default {
  data() {
    return {
      fileList: [],
      previewURL: '',
      isShowDialog: false,
      percent: 0,
      isShowPercent: false
    }
  },
  methods: {
    // 预览
    onPreview(file) {
      console.log('预览', file)
      this.previewURL = file.url
      this.isShowDialog = true
    },
    // 修改
    onChange(file, fileList) {
      console.log(44, file, fileList)
      this.fileList = fileList
    },
    // 删除
    onRemove(file, fileList) {
      this.fileList = fileList
    },
    // 限制数量
    onExceed(files, fileList) {
      // console.log('提示限制图片数量', files, fileList)
      this.$message.warning('封面只能上传一个')
    },
    // 上传前校验
    beforeUpload(file) {
      console.log(53, file)
      // 校验格式
      const whiteList = ['image/png', 'image/jpeg']
      if (!whiteList.includes(file.type)) {
        this.$message.warning('暂时只支持jpeg、png的格式')
        return false
      }
      // 校验大小
      const maxSize = 2 * 1024 * 1024
      if (file.size > maxSize) {
        this.$message.warning('图片大小不能超过2M')
        return false
      }
    },
    // 上传
    upload(params) {
      this.isShowPercent = true
      console.log('上传')
      console.log(83, params)
      cos.putObject({
        // 储存桶名
        Bucket: 'hr-1308572228', // 自己的
        // 区域标志
        Region: 'ap-nanjing', // 自己的
        // 文件名
        Key: params.file.name,
        // 固定写法, 标准储存的意思
        StorageClass: 'STANDARD',
        // 文件对象本身
        Body: params.file,
        onProgress: (progressData) => {
          console.log(JSON.stringify(progressData))

          this.percent = Math.round(progressData.percent * 100)
        }
      }, (err, data) => {
        console.log(err || data)
        this.fileList[0].url = 'http://' + data.Location
        this.fileList[0].status = 'success'

        setTimeout(() => {
          this.isShowPercent = false
        }, 1000)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.disable{
 ::v-deep .el-upload--picture-card{
    display: none
 }
}
</style>
